home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 11 / Mac Magazin and MacEasy Magazine CD - Issue 11.iso / Sharewarebibliothek / Entwickler / WASTE 1.1b1 Distribution / Demo Source / WEDemoEvents.p < prev    next >
Text File  |  1995-06-01  |  11KB  |  454 lines

  1. unit WEDemoEvents;
  2.  
  3. { WASTE DEMO PROJECT: }
  4. { Events Handling }
  5.  
  6. { Copyright © 1993-1995 Marco Piovanelli }
  7. { All Rights Reserved }
  8.  
  9. interface
  10.     uses
  11.         WEDemoIntf;
  12.  
  13.     function InitializeEvents: OSErr;
  14.     procedure DoWindowEvent (var event: EventRecord);
  15.     procedure ProcessEvent;
  16.  
  17. implementation
  18.     uses
  19.         AppleEvents, AERegistry, DiskInit, TextServices, WEDemoMenus, WEDemoScripting, WEDemoWindows, Segments;
  20.  
  21.     const
  22.  
  23. { possible values for HandleOpenDocument refCon parameter }
  24.  
  25.         kDoOpen = 0;
  26.         kDoPrint = 1;
  27.  
  28.     var
  29.  
  30.         gSleepTime: LongInt;                    { sleep time for WaitNextEvent }
  31.         gMouseRgn: RgnHandle;                { mouse region for WaitNextEvent }
  32.  
  33.     procedure AdjustCursor (mouseLoc: Point;
  34.                                     mouseRgn: RgnHandle);
  35.         var
  36.             window: WindowPtr;
  37.     begin
  38.  
  39. { by default, set mouseRgn to the whole QuickDraw coordinate plane, }
  40. { so that we never get mouse moved events }
  41.         SetRectRgn(mouseRgn, -maxint, -maxint, maxint, maxint);
  42.  
  43. { give text services a chance to set the cursor shape }
  44.         if (gHasTextServices) then
  45.             if (SetTSMCursor(mouseLoc)) then
  46.                 Exit(AdjustCursor);
  47.  
  48. { if there is a window open, give WEAdjustCursor an opportunity to set the cursor }
  49. { WEAdjustCursor intersects mouseRgn (if supplied) with a region within which }
  50. { the cursor is to retain its shape. }
  51. { (if the cursor is outside the view region, this is subtracted from mouseRgn) }
  52.  
  53.         window := FrontWindow;
  54.         if (window <> nil) then
  55.             if (WEAdjustCursor(mouseLoc, mouseRgn, DocumentPeek(window)^.hWE)) then
  56.                 Exit(AdjustCursor);
  57.  
  58. { set the cursor to the arrow cursor }
  59. {$IFC NOT UNDEFINED THINK_PASCAL}
  60.         SetCursor(arrow);
  61. {$ELSEC}
  62.         SetCursor(qd.arrow);
  63. {$ENDC}
  64.  
  65.     end;  { AdjustCursor }
  66.  
  67.     procedure DoMouseDown (var event: EventRecord);
  68.         var
  69.             window: WindowPtr;
  70.             partCode: Integer;
  71.     begin
  72.  
  73. { find out where this click went down in }
  74.         partCode := FindWindow(event.where, window);
  75.  
  76. { dispatch on partCode }
  77.         case partCode of
  78.  
  79.             inMenuBar: 
  80.                 begin
  81.                     PrepareMenus;
  82.                     DoMenuChoice(MenuSelect(event.where));
  83.                 end;
  84.  
  85.             inSysWindow: 
  86.                 SystemClick(event, window);
  87.  
  88.             inContent: 
  89.                 if (DoContent(event.where, event, window)) then
  90.                     SelectWindow(window);
  91.  
  92.             inDrag: 
  93.                 DoDrag(event.where, window);
  94.  
  95.             inGrow: 
  96.                 DoGrow(event.where, window);
  97.  
  98.             inGoAway: 
  99.                 if (TrackGoAway(window, event.where)) then
  100.                     if (DoClose(closingWindow, savingAsk, window) <> noErr) then
  101.                         ;
  102.  
  103.             inZoomIn, inZoomOut: 
  104.                 if (TrackBox(window, event.where, partCode)) then
  105.                     DoZoom(partCode, window);
  106.  
  107.             otherwise
  108.                 ;
  109.         end;  { case partCode }
  110.     end;  { DoMouseDown }
  111.  
  112.     procedure DoKeyDown (var event: EventRecord);
  113.         const
  114.  
  115. { virtual key codes generated by some function keys }
  116.             keyF1 = $7A;
  117.             keyF2 = $78;
  118.             keyF3 = $63;
  119.             keyF4 = $76;
  120.         var
  121.             key: Char;
  122.     begin
  123.  
  124. { extract character code from event message }
  125.         key := Char(BAND(event.message, charCodeMask));
  126.  
  127. { map function keys to the equivalent command+key combos }
  128. { note that all function keys generate the same character code, i.e. $10 }
  129.         if (key = CHR($10)) then
  130.             begin
  131.                 event.modifiers := BOR(event.modifiers, cmdKey);
  132.                 case BSR(BAND(event.message, keyCodeMask), 8) of
  133.  
  134.                     keyF1: 
  135.                         key := 'z';
  136.  
  137.                     keyF2: 
  138.                         key := 'x';
  139.  
  140.                     keyF3: 
  141.                         key := 'c';
  142.  
  143.                     keyF4: 
  144.                         key := 'v';
  145.  
  146.                     otherwise
  147.                         key := CHR(0);
  148.                 end;  { case }
  149.             end;
  150.  
  151. { command + printable character combos are routed to MenuKey }
  152.         if (BAND(event.modifiers, cmdKey) <> 0) and (key >= CHR(32)) then
  153.             begin
  154.                 PrepareMenus;
  155.                 DoMenuChoice(MenuKey(key));
  156.             end
  157.         else
  158.             DoKey(key, event);
  159.     end;  { DoKeyDown }
  160.  
  161.     procedure DoDiskEvent (var event: EventRecord);
  162.         var
  163.             dialogCorner: Point;
  164.             err: OSErr;
  165.     begin
  166.         if (BSR(event.message, 16) <> noErr) then
  167.             begin
  168.                 SetPt(dialogCorner, 112, 80);
  169.                 err := DIBadMount(dialogCorner, event.message);
  170.             end;
  171.     end;  { DoDiskEvent }
  172.  
  173.     procedure DoOSEvent (var event: EventRecord);
  174.         var
  175.             osMessage: Integer;
  176.             window: WindowPtr;
  177.     begin
  178.  
  179. { extract the OS message field from the event record }
  180.         osMessage := BSR(BAND(event.message, osEvtMessageMask), 24);
  181.  
  182. { dispatch on osMessage }
  183.         case osMessage of
  184.  
  185.             suspendResumeMessage: 
  186.                 begin
  187.                     window := FrontWindow;
  188.                     if (window <> nil) then
  189.                         DoActivate(BAND(event.message, resumeFlag) <> 0, window);
  190.                 end;
  191.  
  192.             mouseMovedMessage: 
  193.                 ;
  194.  
  195.             otherwise
  196.                 ;
  197.         end;  { case }
  198.     end;  { DoOSEvent }
  199.  
  200.     procedure DoHighLevelEvent (var event: EventRecord);
  201.         var
  202.             err: OSErr;
  203.     begin
  204.         err := AEProcessAppleEvent(event);
  205.     end;  { DoHighLevelEvent }
  206.  
  207.     procedure DoNullEvent (var event: EventRecord);
  208.         var
  209.             window: WindowPtr;
  210.     begin
  211.         window := FrontWindow;
  212.         if (window <> nil) then
  213.             WEIdle(gSleepTime, DocumentPeek(window)^.hWE)
  214.         else
  215.             gSleepTime := maxLongInt;
  216.  
  217. { unload (= unlock & mark as purgeable) non-persistent code segments }
  218.         UnloadNonPersistentSegments;
  219.  
  220.     end;  { DoNullEvent }
  221.  
  222.     procedure DoWindowEvent (var event: EventRecord);
  223.         var
  224.             window: WindowPtr;
  225.     begin
  226.  
  227. { the message field of the event record contains the window pointer }
  228.         window := WindowPtr(event.message);
  229.  
  230. { make sure this window is an application window; check the windowKind field }
  231.         if (WindowPeek(window)^.windowKind <> userKind) then
  232.             Exit(DoWindowEvent);
  233.  
  234.         case event.what of
  235.  
  236.             updateEvt: 
  237.                 DoUpdate(window);
  238.  
  239.             activateEvt: 
  240.                 DoActivate(BAND(event.modifiers, activeFlag) <> 0, window);
  241.  
  242.         end;  { case }
  243.     end;  { DoWindowEvent }
  244.  
  245.     procedure ProcessEvent;
  246.         const
  247.             kHighLevelEvent = 23;        { copied from "EPPC.p" }
  248.         var
  249.             event: EventRecord;
  250.             gotEvent: Boolean;
  251.     begin
  252.  
  253.         gotEvent := WaitNextEvent(everyEvent, event, gSleepTime, gMouseRgn);
  254.  
  255. { give text services a chance to intercept this event }
  256.         if (gHasTextServices) then
  257.             if TSMEvent(event) then
  258.                 ;
  259.  
  260. { adjust cursor shape and set mouse region }
  261. { (we assume event.where is the current mouse position in global coordinates }
  262. { if event.what <= osEvt; high-level events store the event ID there) }
  263.  
  264.         if (event.what <= osEvt) then
  265.             AdjustCursor(event.where, gMouseRgn);
  266.  
  267. { dispatch on event.what }
  268.         case event.what of
  269.  
  270.             nullEvent: 
  271.                 DoNullEvent(event);
  272.  
  273.             mouseDown: 
  274.                 DoMouseDown(event);
  275.  
  276.             keyDown, autoKey: 
  277.                 DoKeyDown(event);
  278.  
  279.             updateEvt, activateEvt: 
  280.                 DoWindowEvent(event);
  281.  
  282.             diskEvt: 
  283.                 DoDiskEvent(event);
  284.  
  285.             osEvt: 
  286.                 DoOSEvent(event);
  287.  
  288.             kHighLevelEvent: 
  289.                 DoHighLevelEvent(event);
  290.  
  291.             otherwise
  292.                 ;                                            { ignore other events }
  293.         end;  { case }
  294.  
  295.         if (gotEvent) then
  296.             gSleepTime := 0;                        { force early idle after non-idle event }
  297.  
  298.     end;  { ProcessEvent }
  299.  
  300.     function GotRequiredParams (ae: AppleEvent): OSErr;
  301.         var
  302.             returnedType: DescType;
  303.             actualSize: Size;
  304.             err: OSErr;
  305.     begin
  306.         err := AEGetAttributePtr(ae, keyMissedKeywordAttr, typeWildCard, returnedType, nil, 0, actualSize);
  307.         if (err = errAEDescNotFound) then
  308.             GotRequiredParams := noErr
  309.         else if (err = noErr) then
  310.             GotRequiredParams := errAEParamMissed
  311.         else
  312.             GotRequiredParams := err;
  313.     end;  { GotRequiredParams }
  314.  
  315.     function HandleOpenDocument (ae, reply: AppleEvent;
  316.                                     refCon: LongInt): OSErr;
  317.         var
  318.             docList: AEDescList;
  319.             keyword: AEKeyword;
  320.             returnedType: DescType;
  321.             actualSize: Size;
  322.             numberOfDocuments, i: LongInt;
  323.             fileSpec: FSSpec;
  324.  
  325.         procedure CheckErr (err: OSErr);
  326.         begin
  327.             if (err <> noErr) then
  328.                 begin
  329.                     HandleOpenDocument := err;
  330.                     err := AEDisposeDesc(docList);
  331.                     Exit(HandleOpenDocument);
  332.                 end;
  333.         end;  { CheckErr }
  334.  
  335.     begin
  336.         HandleOpenDocument := noErr;
  337.  
  338. { extract direct parameter from the Apple Event }
  339.         CheckErr(AEGetParamDesc(ae, keyDirectObject, typeAEList, docList));
  340.  
  341. { perform the recommended check for additional required parameters }
  342.         CheckErr(GotRequiredParams(ae));
  343.  
  344. { count the items in the list of aliases }
  345.         CheckErr(AECountItems(docList, numberOfDocuments));
  346.  
  347.         for i := 1 to numberOfDocuments do
  348.             begin
  349.  
  350. { coerce the nth alias to a file system specification record }
  351.                 CheckErr(AEGetNthPtr(docList, i, typeFSS, keyword, returnedType, @fileSpec, SizeOf(fileSpec), actualSize));
  352.  
  353. { open the specified file }
  354.                 CheckErr(CreateWindow(@fileSpec));
  355.             end;  { for }
  356.  
  357. { dispose of the alias list }
  358.         CheckErr(AEDisposeDesc(docList));
  359.  
  360.     end;  { HandleOpenDocument }
  361.  
  362.     function HandleOpenApplication (ae, reply: AppleEvent;
  363.                                     refCon: LongInt): OSErr;
  364.         var
  365.             err: OSErr;
  366.     begin
  367.  
  368. { perform the recommended check for additional required parameters }
  369.         err := GotRequiredParams(ae);
  370.         if (err <> noErr) then
  371.             begin
  372.                 HandleOpenApplication := err;
  373.                 Exit(HandleOpenApplication);
  374.             end;
  375.  
  376. { create a new window from scratch }
  377.         HandleOpenApplication := CreateWindow(nil);
  378.  
  379.     end;  { HandleOpenApplication }
  380.  
  381.     function HandleQuitApplication (ae, reply: AppleEvent;
  382.                                     refCon: LongInt): OSErr;
  383.         var
  384.             optKey: AEKeyword;
  385.             actualType: DescType;
  386.             actualSize: Size;
  387.             saving: SavingOption;
  388.             err: OSErr;
  389.     begin
  390.  
  391. { default saving option is savingAsk }
  392.         saving := savingAsk;
  393.  
  394. { extract the optional save options }
  395.         err := AEGetParamPtr(ae, keyAESaveOptions, typeEnumerated, actualType, @optKey, SizeOf(optKey), actualSize);
  396.         if (err = noErr) then
  397.             begin
  398.                 if (optKey = kAEYes) then
  399.                     saving := savingYes
  400.                 else if (optKey = kAENo) then
  401.                     saving := savingNo
  402.                 else if (optKey <> kAEAsk) then
  403.                     begin
  404.                         HandleQuitApplication := paramErr;        { for want of a better code }
  405.                         Exit(HandleQuitApplication);
  406.                     end;
  407.             end;
  408.  
  409. { perform the recommended check for additional required parameters }
  410.         err := GotRequiredParams(ae);
  411.         if (err <> noErr) then
  412.             begin
  413.                 HandleQuitApplication := err;
  414.                 Exit(HandleQuitApplication);
  415.             end;
  416.  
  417.         HandleQuitApplication := DoQuit(saving);
  418.     end;  { HandleQuitApplication }
  419.  
  420. { THINK Pascal compiler directive: put the following code in the "Init" segment }
  421. {$S Init}
  422.  
  423.     function InitializeEvents: OSErr;
  424.  
  425.         procedure CheckErr (err: OSErr);
  426.         begin
  427.             if (err <> noErr) then
  428.                 begin
  429.                     InitializeEvents := err;
  430.                     Exit(InitializeEvents);
  431.                 end;
  432.         end;  { CheckErr }
  433.  
  434.     begin
  435.         InitializeEvents := noErr;
  436.  
  437. { allocate space for the mouse region }
  438.         gMouseRgn := NewRgn;
  439.  
  440. { install Apple event handlers for the Required Suite }
  441.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(@HandleOpenApplication), 0, false));
  442.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc(@HandleOpenDocument), kDoOpen, false));
  443.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerProc(@HandleOpenDocument), kDoPrint, false));
  444.         CheckErr(AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(@HandleQuitApplication), 0, false));
  445.  
  446. { install Apple event handlers for a subset of the Core Suite }
  447.         CheckErr(InstallCoreHandlers);
  448.  
  449. { install Apple event handlers for inline input }
  450.         CheckErr(WEInstallTSMHandlers);
  451.  
  452.     end;  { InitializeEvents }
  453.  
  454. end.